﻿using System;
using System.Collections.Generic;
using System.IO;
using NUnit.Framework;
using SeleniumToolkit.Config;

namespace SeleniumToolkit.Config
{
    public class UserExtensionElementCollectionSpecs : ContextSpecification
    {
        protected UserExtensionElementCollection config;
        protected string fileName;

        protected override void Context()
        {
            config = new UserExtensionElementCollection();
        }

        protected override void Because()
        {
            fileName = config.GetExtensionsFile();
        }

        protected override void CleanUpContext()
        {
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
        }


        [TestFixture]
        public class WhenExtensionsAreSpecified : UserExtensionElementCollectionSpecs
        {
            List<string> files;

            protected override void Context()
            {
                // create temp javascript files
                CreateTempFiles();

                // create extensions config
                base.Context();

                // add files to config
                int i = 1;
                files.ForEach(file =>
                {
                    config.Add(new UserExtensionElement(i++.ToString(), file));
                });
            }

            protected override void CleanUpContext()
            {
                base.CleanUpContext();

                CleanupTempFiles();
            }

            [Test]
            public void ShouldCombineFilesIntoTempFile()
            {
                string[] lines = File.ReadAllLines(fileName);
                lines.Length.ShouldEqual(4);
                lines[1].ShouldContain("file1.js");
                lines[3].ShouldContain("file2.js");
            }

            [Test]
            public void ShouldProduceFile()
            {
                fileName.ShouldNotBeNull();
                File.Exists(fileName).ShouldBeTrue();
            }

            [Test]
            public void CanCleanupFile()
            {
                File.Exists(fileName).ShouldBeTrue();
                config.CleanupTempFile();
                File.Exists(fileName).ShouldBeFalse();
            }

            #region helper methods
            private void CreateTempFiles()
            {
                int i = 1;
                files = new List<string>();
                List<string> fileNames = new List<string>() { "file1.js", "file2.js" };
                fileNames.ForEach(file =>
                {
                    file = Path.Combine(Environment.CurrentDirectory, file);
                    using (StreamWriter writer = new StreamWriter(file))
                    {
                        writer.Write(String.Format("\\ {0}", file));
                        writer.Write(String.Format("var i{0} = '{1}';", i++, file));
                        writer.Flush();
                    }
                    files.Add(file);
                });
            }

            private void CleanupTempFiles()
            {
                files.ForEach(file =>
                {
                    if (File.Exists(file))
                        File.Delete(file);
                });
            }
            #endregion
        }

        [TestFixture]
        public class WhenNoExtensionsSpecified : UserExtensionElementCollectionSpecs
        {
            [Test]
            public void ShouldNotHaveFileName()
            {
                fileName.ShouldBeNull();
            }
        }
    }

}
